package com.joysuch.wwyt.mh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.joysuch.wwyt.core.enums.DeleteFlag;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.mh.entity.ScreenMapConfig;
import com.joysuch.wwyt.mh.mapper.ScreenMapConfigMapper;
import com.joysuch.wwyt.mh.service.ScreenMapConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author heqifeng
 * @since 2021-07-14
 */
@Service
public class ScreenMapConfigServiceImpl extends ServiceImpl<ScreenMapConfigMapper, ScreenMapConfig> implements ScreenMapConfigService {

    @Autowired
    private ScreenMapConfigMapper screenMapConfigMapper;

    /**
     * 获取当前公司的大屏配置信息
     * @return
     */
    @Override
    public ScreenMapConfig getConfig() {
        LambdaQueryWrapper<ScreenMapConfig> queryWrapper = new LambdaQueryWrapper<ScreenMapConfig>().last("limit 1");
        String orgCode = Context.getCompanyCode();
        Long tenantId = Context.getCurrentTenantId();
        queryWrapper.eq(ScreenMapConfig::getDeleteFlag, DeleteFlag.EFFECTIVE.getCode())
                    .eq(ScreenMapConfig::getTenentId,tenantId)
                .likeRight(ScreenMapConfig::getOrgCode,orgCode);
        return getOne(queryWrapper);
    }

    @Override
    public boolean saveOrUpdate(ScreenMapConfig screenMapConfig) {
        ScreenMapConfig config = getConfig();
        int count = 0;
        if (config == null){
            count = screenMapConfigMapper.insert(screenMapConfig);
        }else {
            ScreenMapConfig updateConfig = screenMapConfig.setId(config.getId());
            count = screenMapConfigMapper.updateById(updateConfig);
        }
        return count > 0;
    }
}
